
/* Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
* file dvpp_engine.h
* version 1.0
* date 2018-4-25
*/

#ifndef BATCH_IMAGE_PARA_WITH_SCALE_H
#define BATCH_IMAGE_PARA_WITH_SCALE_H

#include "hiaiengine/data_type.h"
#include "hiaiengine/data_type_reg.h"
#include "hiaiengine/status.h"
#include <unistd.h>
#include <sys/stat.h>
#include "hiaiengine/ai_tensor.h"

using hiai::BatchInfo;
using hiai::IMAGEFORMAT;
using hiai::ImageData;

/**
define error code for HIAI_ENGINE_LOG
**/
const int USE_DEFINE_ERROR = 0x6001;

enum {
    HIAI_IDE_ERROR_CODE,
    HIAI_IDE_INFO_CODE,
    HIAI_IDE_WARNING_CODE
};

HIAI_DEF_ERROR_CODE(USE_DEFINE_ERROR, HIAI_ERROR, HIAI_IDE_ERROR,
                    "");
HIAI_DEF_ERROR_CODE(USE_DEFINE_ERROR, HIAI_INFO, HIAI_IDE_INFO,
                    "");
HIAI_DEF_ERROR_CODE(USE_DEFINE_ERROR, HIAI_WARNING, HIAI_IDE_WARNING,
                    "");

typedef struct ScaleInfo {
    float scale_width = 1;
    float scale_height = 1;
} ScaleInfoT;
template<class Archive>
void serialize(Archive &ar, ScaleInfoT &data);

typedef struct ResizeInfo {
    uint32_t resize_width = 0;
    uint32_t resize_height = 0;
} ResizeInfoT;
template<class Archive>
void serialize(Archive &ar, ResizeInfo &data);

typedef struct CropInfo {
    int point_x = -1;
    int point_y = -1;
    int crop_width = -1;
    int crop_height = -1;
} CropInfoT;
template<class Archive>
void serialize(Archive &ar, CropInfo &data);

typedef struct NewImagePara {
    hiai::FrameInfo f_info;
    hiai::ImageData<u_int8_t> img;
    ScaleInfoT scale_info;
    ResizeInfo resize_info;
    CropInfo crop_info;
} NewImageParaT;

template<class Archive>
void serialize(Archive &ar, NewImageParaT &data);

typedef struct NewImagePara2 {
    hiai::FrameInfo f_info;
    hiai::ImageData<float> img;
    ScaleInfoT scale_info;
} NewImageParaT2;

template<class Archive>
void serialize(Archive &ar, NewImageParaT2 &data);

typedef struct BatchImageParaWithScale {
    hiai::BatchInfo b_info;
    std::vector<NewImageParaT> v_img;
} BatchImageParaWithScaleT;

template<class Archive>
void serialize(Archive &ar, BatchImageParaWithScaleT &data);

struct ImageAll {
    int width_org;
    int height_org;
    int channal_org;
    ImageData<float> image;
};

template<class Archive>
void serialize(Archive &ar, ImageAll &data);

struct BatchImageParaScale {
    BatchInfo b_info;
    std::vector<ImageAll> v_img;
};

template<class Archive>
void serialize(Archive &ar, BatchImageParaScale &data);

typedef enum ImageType {
    IMAGE_TYPE_RAW = -1,
    IMAGE_TYPE_NV12 = 0,
    IMAGE_TYPE_JPEG,
    IMAGE_TYPE_PNG,
    IMAGE_TYPE_BMP,
    IMAGE_TYPE_TIFF,
    IMAGE_TYPE_VIDEO = 100
} ImageTypeT;

struct EvbImageInfo {
    bool is_first;
    bool is_last;
    uint32_t batch_size;
    uint32_t batch_index;
    uint32_t max_batch_size;
    uint32_t batch_ID;
    uint32_t frame_ID;
    int format;
    uint32_t width = 0;
    uint32_t height = 0;
    uint32_t size = 0;
    u_int8_t *pucImageData;
};

const int SEND_DATA_INTERVAL_MS = 200000;
const static mode_t PERMISSION = 0700;
const static mode_t FIlE_PERMISSION = 0600;

// The new version of serialize function
void GetEvbImageInfoSearPtr(void *input_ptr, std::string &ctrl_str, uint8_t *&data_ptr, uint32_t &data_len);

/**
* @brief: check if the image_handle is a sentinel image
* @[in]: image_handle, the image to check
* @[return]: bool, true if the image is a sentinel image
 */
bool IsSentinelImage(const std::shared_ptr<BatchImageParaWithScaleT> image_handle);

// The new version of deserialize function
std::shared_ptr<void> GetEvbImageInfoDearPtr(const char *ctrl_ptr, const uint32_t &ctr_len,
                                             const uint8_t *data_ptr, const uint32_t &data_len);

typedef struct Output {
    int32_t size;
    std::string name;
    std::shared_ptr<u_int8_t> data;
} OutputT;
template<class Archive>
void serialize(Archive &ar, OutputT &data);

typedef struct EngineTrans {
    bool status;
    std::string msg;
    hiai::BatchInfo b_info;
    uint32_t size;
    std::vector<OutputT> output_data_vec;
    std::vector<NewImageParaT> v_img;
} EngineTransT;
template<class Archive>
void serialize(Archive &ar, EngineTransT &data);

typedef struct {
    std::string tfilename;
    int format;
    int height;
    int width;
} ImageInfor;

/**
* @brief: get the result file name from the image name
* @[in]: imgFullPath: the image file path
* @[in]: postfix: the type of the result file
 */
std::string GenTfileName(std::string imgFullPath, std::string postfix);

/**
* @brief: get the image information from the info_file generated by dataset engine
* @[in]: info_file: the info file path
* @[in]: postfix: the type of the result file
 */
std::unordered_map<int, ImageInfor> SetImgPredictionCorrelation(std::string info_file, std::string postfix);

/**
* @brief: get the caffe layer name and index
* @[in]: in_name: the name of tensor
* @[in]: index: the index of output tensor
* @[in]: out_name: the caffe layer name
 */
void GetLayerName(const std::string in_name, std::string &index, std::string &out_name);

/**
* @brief: create folder to store the detection results
* the folder name on the host will be "result_files/enginename"
 */
HIAI_StatusT CreateFolder(std::string folderPath, mode_t mode);

/**
* @brief: get the information file path in the dataset folder
* @[in]: value, path configuration string
* @[return]: string, info file path
 */
std::string GetInfoFilePath(const std::string pathConfig);

/**
* @brief: replace '/' to '_'  in name
* @[in]: name, the name to replace
 */
void GetOutputName(std::string &name);

#endif
